package com.xiaodou.mjf.application.basic.utils;

import com.google.common.base.Strings;
import com.xiaodou.mjf.application.core.service.SequenceService;
import com.xiaodou.mjf.entity.core.Sequence;
import com.xiaodou.mjf.infrastructure.common.context.SpringContextHolder;
import org.apache.commons.lang3.RandomUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

public class SequenceGenerator {


    private static SequenceService sequenceService = SpringContextHolder.getBean(SequenceService.class);

    public static Integer TYPE_DEFAULT_ACCOUNT = 1;
    public static Integer TYPE_LOAN_ACCOUNT = 2;
    public static Integer TYPE_FINANCING = 3;
    public static Integer TYPE_FINANCING_PACKAGE = 4;
    public static Integer TYPE_NOTEBOOK = 5;
    public static Integer TYPE_DEPOSIT = 6;
    public static Integer TYPE_CROWD = 7;
    public static Integer TYPE_BATCH_PAY = 8;

    public static final String DEFAULT_SYMBOL = "0";

    public static String generateAccountNumber(Integer type) {

        Integer serial = 1;
        Sequence sequence = sequenceService.getInfoByType(type);
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setType(type);
            sequence.setValue(1);
        } else {
            serial = sequence.getValue();
            sequence.setValue(sequence.getValue() + 1);
        }

        sequenceService.save(sequence);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("1000");
        stringBuffer.append(type);
        stringBuffer.append("2");
        stringBuffer.append(RandomUtils.nextInt(0, 9));
        stringBuffer.append(Strings.repeat(DEFAULT_SYMBOL, 8 - String.valueOf(serial).length()));
        stringBuffer.append(serial);

        String baseAccount = stringBuffer.toString();
        Integer oddCoefficient = 0, evenCoefficient = 0;

        //基础账号的奇数位ASCII码*2，偶数位ASCII码*3
        for (int i = 0; i < baseAccount.length(); i++) {
            if (i % 2 == 0) {
                evenCoefficient += baseAccount.charAt(i) * 3;
            } else {
                oddCoefficient += baseAccount.charAt(i) * 2;
            }
        }

        Integer dvalue = evenCoefficient - oddCoefficient;
        dvalue = Math.abs(dvalue);

        String validateCode = String.valueOf(dvalue % 100);
        validateCode = Strings.repeat(DEFAULT_SYMBOL, 2 - validateCode.length()) + validateCode;

        stringBuffer.append(validateCode);


        return stringBuffer.toString();
    }

    public static String generateProdCodeNo(Integer type) {

        Integer serial = 1;
        Sequence sequence = sequenceService.getInfoByType((type));
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setType((type));
            sequence.setValue(1);
        } else {
            serial = sequence.getValue();
            sequence.setValue(sequence.getValue() + 1);
        }
        sequenceService.save(sequence);
        //2015+1(类型)+00001----2015+1(类型)+99999
        StringBuffer stringBuffer = new StringBuffer();
        Calendar now = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMM");
        String yymm = dateFormat.format(now.getTime()).toString();
        stringBuffer.append(yymm).append(type);
        stringBuffer.append(Strings.repeat("0", 5 - String.valueOf(serial).length())).append(serial);
        return stringBuffer.toString();
    }

    public static String generateOrder(Integer type) {

        Integer serial = 1;
        Sequence sequence = sequenceService.getInfoByType((type));
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setType((type));
            sequence.setValue(1);
        } else {
            serial = sequence.getValue();
            sequence.setValue(sequence.getValue() + 1);
        }
        sequenceService.save(sequence);
        //"10"yyyyMMddHHmm+000001
        StringBuffer stringBuffer = new StringBuffer();
        Calendar now = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        String yymm = dateFormat.format(now.getTime()).toString();
        stringBuffer.append("10");
        stringBuffer.append(yymm);
        stringBuffer.append(Strings.repeat("0", 6 - String.valueOf(serial).length())).append(serial);
        return stringBuffer.toString();
    }

    public static String generateNotesBook(Integer type, String channelNo) {

        Integer serial = 1;
        Sequence sequence = sequenceService.getInfoByType((type));
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setType((type));
            sequence.setValue(1);
        } else {
            serial = sequence.getValue();
            sequence.setValue(sequence.getValue() + 1);
        }
        sequenceService.save(sequence);
        //流水规则：
        // 20150322   10210001   00   			000000001
        // 当前日期   渠道号	  两位随机码	流水号，保存于sequence表中
        StringBuffer stringBuffer = new StringBuffer();
        DateTime dt = new DateTime();
        stringBuffer.append(dt.toString(DateTimeFormat.forPattern("yyyyMMdd"))).append(channelNo);
        Random random = new Random();
        int youNumber = random.nextInt(100);
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        stringBuffer.append(String.format("%02d", youNumber));
        stringBuffer.append(serial);
        return stringBuffer.toString();
    }

    public static String generateCrowd(Integer type, String channelNo) {

        Integer serial = 1;
        Sequence sequence = sequenceService.getInfoByType((type));
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setType((type));
            sequence.setValue(1);
        } else {
            serial = sequence.getValue();
            sequence.setValue(sequence.getValue() + 1);
        }
        sequenceService.save(sequence);
        //流水规则：
        // 20150322   10210001   00   			000000001
        // 当前日期   渠道号	  两位随机码	流水号，保存于sequence表中
        StringBuffer stringBuffer = new StringBuffer();
        DateTime dt = new DateTime();
        stringBuffer.append(dt.toString(DateTimeFormat.forPattern("yyyyMMdd"))).append(channelNo);
        Random random = new Random();
        int youNumber = random.nextInt(100);
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        stringBuffer.append(String.format("%02d", youNumber));
        stringBuffer.append(serial);
        return stringBuffer.toString();
    }

    public static String generateBatchPay(String comName) {

        int type = TYPE_BATCH_PAY;
        DateTime dt = new DateTime();

        Integer serial = 1;
        Sequence sequence = sequenceService.getInfoByType((type));
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setType((type));
            sequence.setValue(1);
        } else {
            serial = sequence.getValue();
            sequence.setValue(sequence.getValue() + 1);
        }
        sequenceService.save(sequence);
        //流水规则：
        //公司简称缩写+yymmdd+流水号
        StringBuffer stringBuffer = new StringBuffer(comName);
        stringBuffer.append(dt.toString(DateTimeFormat.forPattern("yyyyMMdd"))).append(serial);
        return stringBuffer.toString();
    }

    public static String generateCrowdNo(Integer type) {

        Integer serial = 1;
        Sequence sequence = sequenceService.getInfoByType((type));
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setType((type));
            sequence.setValue(1);
        } else {
            serial = sequence.getValue();
            sequence.setValue(sequence.getValue() + 1);
        }
        sequenceService.save(sequence);
        //流水规则：
        // 20150322   10210001   00   			000000001
        // 当前日期   渠道号	  两位随机码	流水号，保存于sequence表中
        StringBuffer stringBuffer = new StringBuffer();
        DateTime dt = new DateTime();
        stringBuffer.append(dt.toString(DateTimeFormat.forPattern("yyyyMMdd"))).append(serial);
        Random random = new Random();
        int youNumber = random.nextInt(100);
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        stringBuffer.append(String.format("%02d", youNumber));
        stringBuffer.append(serial);
        return stringBuffer.toString();
    }
}


